package JDK常用类库.序列化和反序列化;

import java.io.*;

public class StudentClone implements Serializable {
    public static Student myClone(Student student){
        Student anotherStu = null;
        try {
            //在内存中开辟一块缓冲区，将对象序列化成流
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(bout);
            out.writeObject(student);

            //找到这一块缓冲区，将字节流反序列化成另一个对象
            ByteArrayInputStream bais = new ByteArrayInputStream(bout.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bais);
            anotherStu = (Student) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return anotherStu;
    }
}
